<?php

class PageController extends Controller {
	
  public function __construct($id, $module = null) {
    parent::__construct ( $id, $module );
    $this->layout = 'application.views.layouts.layout';
  }
  public function filters() {
    return array ('accessControl' );
  }
  public function accessRules() {
    return array (array ('allow', 'actions' => array ('index', 'card' ), 'users' => array ('*' ) ), array ('deny', 'expression' => '$user->isGuest' ) );
  }
  public function actionIndex($uid = null) {
    $page = $this->loadModel($uid);
    $this->render ( 'index', array ('page' => $page ) );
  }
  
  public function actionEdit(){
  	
  	$page = $this->loadModel();
    $this->render ( 'edit', array ('page' => $page ) );
    
  }
  private function loadModel($uid = null){
	  if ($uid === null){
	      if (! Yii::app ()->user->isGuest) {
	        //          $this->forward('edit');
	        $uid = Yii::app ()->user->uid;
	      } else {
	        throw new CException ( "No such page." );
	      }
	    }
      
//      $uid = Yii::app()->user->uid;
     return Page::model ()->findFirstPageByUid ( $uid );
  
  }
  public function actionSave($pageid = null){
	$widgets=$_POST ['widgets'];
	 // print_r($widgets);
	$uid = Yii::app ()->user->uid;
	$page = Page::model ()->findByUid ( $uid,$_POST ['pageNo'] );
	$i=0;	
		if ($page==null) {
			$page=new Page();
			$page->uid=$uid; 
			$page->pageno=$_POST ['pageNo'];
			$page->createdate=date('Y-m-d H:i:s');
			$page->updatedate=null;
			$page->widgets=array();
			foreach ($widgets as $widget) {
				$w=new Widget();
				$w->wid=$widget['wid'];
				$w->classname=$widget['classname'];

				if($widget['classname']==='Page')
					$page->pid=$widget['wid'];
				
				$w->cntitle=$widget['cntitle'];
				$w->viewperm=$widget['viewperm'];
				$w->cmtperm=$widget['cmtperm'];
				$w->tranperm=$widget['tranperm'];
				$w->trandate=$widget['trandate'];
				$w->orig_wid=$widget['orig_wid'];
				$w->createdate=date('Y-m-d H:i:s');
				$w->updatedate=null;
// 			
				$s=new Style();
				@$w->style->wid=$widget['style']['wid'];				
				@$w->style->font=$widget['style']['font'];
				@$w->style->color=$widget['style']['color'];
				@$w->style->width=$widget['style']['width'];
				@$w->style->height=$widget['style']['height'];
				@$w->style->left=$widget['style']['left'];
				@$w->style->top=$widget['style']['top'];
				@$w->style->background=$widget['style']['background'];
				@$w->style->border=$widget['style']['border'];
				@$w->style->position=$widget['style']['position'];
				@$w->style->opacity=$widget['style']['opacity'];
				@$w->style->zindex=$widget['style']['z_index'];

				@$w->attribute->wid=$widget['attribute']['wid'];
				@$w->attribute->weiboId=$widget['attribute']['weiboId'];
				@$w->attribute->bg_pid=$widget['attribute']['bg_pid'];
				@$w->attribute->uid=$widget['attribute']['uid'];
				@$w->attribute->icon_pid=$widget['attribute']['icon_pid'];
				@$w->attribute->text=$widget['attribute']['text'];
				// $w->attribute->attribute=$a;

					 array_push($page->widgets,$w);		
				}  
			
			 $page->save();
		}
		 // Yii::app()->end();
	
	}
  
// public function actionEdited($pageid = null){
//   	
	// print_r($_REQUEST['styles']);
		// if (! empty ( $_POST )) {
// 				
			// $page = null;
			// if ($pageid) {
// 			
			// } else {
				// $uid = Yii::app ()->user->uid;
				// $page = Page::model ()->findByUid ( $uid );
// 			
			// }
// 			
			// if ($page == null) {
				// $page = new Page ();
				// $page->uid = Yii::app ()->user->uid;
				// $page->save();
			// }
// 	   			
// 	   			
// 			
			// if (isset ( $_POST ['id'] ) && isset($_POST['op']) && $_POST['op'] == 'del') { //delete 
				// JWidget::model()->deleteByPk(new MongoId($_POST ['id']));
				// unset($page->widgetIds [$_POST ['id']]);
				// $page->save();
// 
			// } else {
				// $widget = null;
				// if(isset($_POST ['id']) && $_POST ['id'] !== null){
					// $oldWidget = JWidget::model()->findByPk(new MongoId($_POST ['id']));
					// if($oldWidget !== null){
						// $widget = $oldWidget;
					// }
				// }
// 				
				// if($widget === null){
	   				// $widget = new JWidget;
					// $widget->uid = Yii::app ()->user->uid;
				// }
// 				
// //	   			$widget = CMap::mergeArray($widget,$_POST);
// 	   			
	   			// $widget->setAttributes($_POST,false);
	   			// $isNew =  $widget->isNewRecord;
//           
	   			// if($widget->save() && $isNew){
	   				// $page->widgetIds [$widget->_id.''] = time();
					// $page->styles=$_REQUEST['styles'];
	   				// $page->save();
	   			// }
// 
            // $user = User::model()->findByPk(new MongoId($widget->uid));
            // $user->name=$_POST ['name'];
            // $user.save();
// 
	   			// echo json_encode(array('id'=>$widget->_id.'','name'=>$_POST['name']));
			// }
		// }
		// Yii::app()->end();
// 	
	// }
} 
	